<template>
  <div class="article-container">
    <van-nav-bar
      class="app-nav-bar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <div class="main-wrap">
      <h1 class="title">{{ article.title }}</h1>
      <van-cell class="user-info" center>
        <van-image
          class="avatar"
          slot="icon"
          fit="cover"
          round
          :src="article.aut_photo"
        />
        <div class="name" slot="title">{{ article.aut_name }}</div>
        <div class="pubdate" slot="label">{{ article.pubdate | relativeTime}}</div>
        <van-button
          class="follow-btn"
          round
          size="small"
          :type="article.is_followed ? 'default' : 'info'"
          :icon="article.is_followed ? '' : 'plus'"
          :loading="isFollowLoading"
          @click="onFollow"
        >{{ article.is_followed ? '已关注' : '关注'}}</van-button>
      </van-cell>
      <div class="markdown-body"
        v-html="article.content"
        ref="article-content"
      ></div>
      <comment-list
        :source="articleId"
        type="a"
        :list="commentList"
        @reply-click="onReplyClick"
        @update-total-count="commentCount = $event"
      ></comment-list>
    </div>
    <div class="article-bottom">
      <van-button
        class="comment-btn"
        type="default"
        round
        size="small"
        @click="isPopupShow = true"
      >写评论</van-button>
      <van-icon
        name="comment-o"
        :info="commentCount"
        color="#777777"
      ></van-icon>
      <van-icon
        :color="article.is_collected ? 'red' : '#777777'"
        :name="article.is_collected ? 'star' : 'star-o'"
        :loading="isCollectLoading"
        @click="onCollect"
      ></van-icon>
      <van-icon
        :color="article.attitude === 1 ? 'red' : '#777777'"
        :name="article.attitude === 1 ? 'good-job' : 'good-job-o'"
        :loading="isLikeLoading"
        @click="onLike"
      ></van-icon>
      <van-icon
        color="#777777"
        name="share"
      ></van-icon>
    </div>
    <van-popup
      v-model="isPopupShow"
      get-container="body"
      position="bottom"
    ><post-comment
      :target="articleId"
      :article-id="articleId"
      @post-success='onPostSuccess'
    ></post-comment></van-popup>
    <van-popup
      v-model="isReplyShow"
      get-container="body"
      position="bottom"
    >
      <comment-reply v-if="isReplyShow" :comment="replyComment" :article-id="articleId" @close="isReplyShow = false"></comment-reply>
    </van-popup>
  </div>
</template>

<script>
import './github-markdown.css'
import { getArticle, addCollect, cancelCollect, addLike, cancelLike } from '@/api/article.js'
import { addFollow, cancelFollow } from '@/api/user.js'
import { ImagePreview } from 'vant'
import CommentList from './components/comment-list.vue'
import PostComment from './components/post-comment.vue'
import CommentReply from './components/comment-reply.vue'
export default {
  name: 'ArticleIndex',
  props: {
    articleId: {
      type: [String, Number, Object],
      required: true
    }
  },
  components: {
    CommentList,
    PostComment,
    CommentReply
  },
  data () {
    return {
      article: {},
      isFollowLoading: false,
      isCollectLoading: false,
      isLikeLoading: false,
      isPopupShow: false,
      commentList: [],
      commentCount: 0,
      isReplyShow: false,
      replyComment: {}
    }
  },
  computed: {},
  watch: {},
  methods: {
    async loadArticle () {
      const { data } = await getArticle(this.articleId)
      this.article = data.data
      this.$nextTick(() => {
        this.handlePreviewImage()
      })
    },
    handlePreviewImage () {
      const articleContent = this.$refs['article-content']
      const imgs = articleContent.querySelectorAll('img')
      const imgPaths = []
      imgs.forEach((img, index) => {
        imgPaths.push(img.src)
        img.onClick = function () {
          ImagePreview({
            images: imgPaths,
            startPosition: index
          })
        }
      })
    },
    async onFollow () {
      this.isFollowLoading = true
      if (this.article.is_followed) {
        await cancelFollow(this.article.aut_id)
      } else {
        await addFollow(this.article.aut_id)
      }
      this.article.is_followed = !this.article.is_followed
      this.isFollowLoading = false
    },
    async onCollect () {
      this.isCollectLoading = true
      if (this.article.is_collected) {
        await cancelCollect(this.articleId)
      } else {
        await addCollect(this.articleId)
      }
      this.article.is_collected = !this.article.is_collected
      this.isCollectLoading = false
    },
    async onLike () {
      this.isLikeLoading = true
      if (this.article.attitude === 1) {
        await cancelLike(this.articleId)
        this.article.attitude = 0
      } else {
        await addLike(this.articleId)
        this.article.attitude = 1
      }
      this.isLikeLoading = false
    },
    onPostSuccess (comment) {
      this.commentList.unshift(comment)
      this.commentCount++
      this.isPopupShow = false
    },
    onReplyClick (comment) {
      this.replyComment = comment
      this.isReplyShow = true
    }
  },
  created () {
    this.loadArticle()
  },
  mounted () {
  },
  destroyed () {
  }
}
</script>

<style scoped lang='less'>
.article-container {
  .main-wrap {
    height: 86vh;
    position: fixed;
    left: 0;
    right: 0;
    top: 46px;
    bottom: 50px;
    overflow-y: auto;
    .title {
      font-size: 20px;
      color: #3a3a3a;
      padding: 14px 14px;
      background-color: #ffffff;
      margin: 0;
    }
    .user-info {
      .avatar {
        width: 35px;
        height: 35px;
        margin-right: 8px;
      }
      .name {
        font-size: 12px;
        color: #333333;
      }
      .pubdate {
        font-size: 12px;
        color: #b4b4b4;
      }
      .follow-btn {
        width: 85px;
        height: 29px;
      }
    }
    ul {
      list-style: unset;
    }
    .markdown-body {
      padding: 14px;
      background-color: #ffffff;
    }
  }
  .article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    // box-sizing: border-box;
    height: 49px;
    border-top: 1px solid #d8d8d8;
    background-color: #ffffff;
    .comment-btn {
      width: 120px;
      height: 30px;
      border: 2px solid #eeeeee;
      font-size: 15px;
      line-height: 46px;
      color: #a7a7a7;
    }
    .van-icon {
      font-size: 25px;
      .van-info {
        font-size: 6px;
        background-color: #e22829;
      }
    }
  }
}
</style>
